﻿using Newtonsoft.Json;
using QWManage.Model;
using QWManage.Utils;
using System.Collections.Generic;

namespace QWManage.API.DepartmentDetail95351
{
    /// <summary>
    /// 获取单个部门详情
    /// </summary>
    internal class DepartmentDetailAPI
    {
        //请求方式：GET（HTTPS）
        //请求地址：https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token=ACCESS_TOKEN&id=ID
        //部门id。
        public static DepartmentInfo Invoke(string token, int id)
        {
            DepartmentInfo departmentInfo = new DepartmentInfo();
            string url = $"https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token={token}&id={id}";
            string result = HttpHelper.GetHtmlSource(url);
            var root = JsonConvert.DeserializeObject<Root>(result);
            departmentInfo.ID = id;
            departmentInfo.PID = root.department.parentid;
            departmentInfo.Name = root.department.name;
            return departmentInfo;
        }
    }

    #region JSON
    public class Department
    {
        /// <summary>
        /// 
        /// </summary>
        public int id { get; set; }
        /// <summary>
        /// 惠丰互动(深圳)科技有限公司
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int parentid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int order { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<string> department_leader { get; set; }
    }

    public class Root
    {
        /// <summary>
        /// 
        /// </summary>
        public int errcode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string errmsg { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Department department { get; set; }
    }
    #endregion
}
